package com.jicheng.service.impl;

import com.jicheng.Utils.ResultMess;
import com.jicheng.dto.app.AppParams;
import com.jicheng.dto.file.UploadDto;
import com.jicheng.ext.dto.FileInfoDto;
import com.jicheng.ext.dto.UploadLocalFileParams;
import com.jicheng.ext.enums.ApiResponseCode;
import com.jicheng.ext.exceptions.BaseException;
import com.jicheng.ext.util.DateUtils;
import com.jicheng.ext.util.FileManager;
import com.jicheng.service.IAppService;
import com.jicheng.service.IFileService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;

@Service
public class FileServiceImpl implements IFileService {

    private Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);
    @Autowired
    private IAppService appService;
    @Override
    public ResultMess uploadApp(MultipartFile file,String version,String comment) {
        notNull("file",file);
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();

        } catch (IOException e) {
            throw new BaseException(ApiResponseCode.数据异常, "获取文件异常");
        }
        String dir = "/usr/app/Downloads/"+ DateUtils.format(new Date(), "yyyyMMddHHmmss") +"/";
        File dest = new File(dir);
        String absolutePath = dest.getAbsolutePath()+"/";
        FileManager fileManager = new FileManager();
        UploadLocalFileParams uploadLocalFileParams = new UploadLocalFileParams();
        uploadLocalFileParams.setFileName(file.getOriginalFilename());
        uploadLocalFileParams.setDir(absolutePath);
        FileInfoDto fileInfoDto = fileManager.uploadLocal(inputStream, uploadLocalFileParams);
        AppParams appParams = new AppParams();
        appParams.setPath(absolutePath+file.getOriginalFilename());
        appParams.setSize(fileInfoDto.getSize().toString());
        appParams.setComment(comment);
        appParams.setVersion(version);
        appService.saveVersion(appParams);
        return new ResultMess(true,"上传成功",appParams);
    }

    @Override
    public ResultMess downloadApp(String path, OutputStream servletOutputStream) {
        notNull("path",path);
        notNull("servletOutputStream",servletOutputStream);
        File file = new File(path);
        FileInputStream inputStream = null;
        try {
            inputStream = FileUtils.openInputStream(file);
            IOUtils.copy(inputStream, servletOutputStream);
            servletOutputStream.flush();
            servletOutputStream.close();
            return new ResultMess(true,"下载成功!",null);
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("读取文件失败：" + e);

//            throw new BaseException(ApiResponseCode.数据异常, e.toString());
            return new ResultMess(false,"下载失败!",null);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    logger.error("io close error:" + e);
                }
            }
        }
    }
}
